//
//  YNRecentChatObejct.m
//  Messenger
//
//  Created by iOS on 15/2/11.
//  Copyright (c) 2015年 孙浪. All rights reserved.
//
//  最近会话实体类
//

#import "YNRecentChatObejct.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FMDatabaseAdditions.h"
#import "YNIMTool.h"
#import "YNIMFrameworkMacro.h"

@implementation YNRecentChatObejct

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if (!value){
        if ([key isEqualToString:@"chatType"]){
            self.chatType=0;
        }else if ([key isEqualToString:@"isTop"]){
            self.isTop=0;
        }else if ([key isEqualToString:@"unReadCount"]){
            self.unReadCount=0;
        }else if ([key isEqualToString:@"sendState"]){
            self.sendState=0;
        }else if ([key isEqualToString:@"messageType"]){
            self.messageType=0;
        }
    }
}

//数据库字段名字
+ (NSString *)ID
{
    return @"ID";
}

+ (NSString *)userNo
{
    return @"userNo";
}

+ (NSString *)title
{
    return @"title";
}

+ (NSString *)content
{
    return @"content";
}

+ (NSString *)chatType
{
    return @"chatType";
}

+ (NSString *)senderName
{
    return @"senderName";
}

+ (NSString *)sendNo
{
    return @"sendNo";
}

+ (NSString *)dataTime
{
    return @"dataTime";
}

+ (NSString *)topTime
{
    return @"topTime";
}

+ (NSString *)headUrl
{
    return @"headUrl";
}

+ (NSString *)headLocalPath
{
    return @"headLocalPath";
}

+ (NSString *)isTop
{
    return @"isTop";
}

+ (NSString *)unReadCount
{
    return @"unReadCount";
}

+ (NSString *)messageBody
{
    return @"messageBody";
}

+ (NSString *)tableName
{
    return @"RecentChat";
}

+ (NSString *)sendState
{
    return @"sendState";
}

+ (NSString *)text
{
    return @"text";
}

+ (NSString *)messageType
{
    return @"messageType";
}

+(NSString *)isNewListPullData
{
    return @"isNewListPullData";
}

+(NSString *)masterRecentType
{
    return @"masterRecentType";
}

+(NSString *)packId
{
    return @"packId";
}

+(NSString *)aitContactMarkArray
{
    return @"aitContactMarkArray";
}

+(NSString *)isMessageWithdraw
{
    return @"isMessageWithdraw";
}

+(NSString *)more{
    return @"more";
}

-(NSInteger)aitContactMark
{
    if(_aitContactMark <=0)
    {
        _aitContactMark = 0 ;
    }
    return _aitContactMark;
}


- (NSArray *)insertParam
{
    return @[[YNIMTool nilFilter:self.title],
             [YNIMTool nilFilter:self.content],
             [NSNumber numberWithInt:self.chatType],
             [YNIMTool nilFilter:self.senderName],
             [YNIMTool nilFilter:self.sendNo],
             [YNIMTool nilFilter:self.dataTime],
             [YNIMTool nilFilter:self.topTime],
             [YNIMTool nilFilter:self.headUrl],
             [YNIMTool nilFilter:self.headLocalPath],
             [NSNumber numberWithInt:self.isTop],
             [NSNumber numberWithInt:self.unReadCount],
             [YNIMTool nilFilter:self.messageBody],
             [NSNumber numberWithInt:self.sendState],
             [YNIMTool nilFilter:self.text],
             [NSNumber numberWithInt:self.messageType],
             [NSNumber numberWithInt:self.isNewListPullData],
             [NSNumber numberWithInt:self.masterRecentType],
             [YNIMTool nilFilter:self.packId],
             [YNIMTool nilFilter:self.aitContactMarkArray],
             [NSNumber numberWithBool:self.isMessageWithdraw],
             [YNIMTool nilFilter:self.more],
             [YNIMTool nilFilter:self.userNo]];
}

- (NSArray *)updateParamIsTop:(BOOL)isTop
{
    if (isTop) {
        return @[[YNIMTool nilFilter:self.title],
                 [YNIMTool nilFilter:self.content],
                 [NSNumber numberWithInt:self.chatType],
                 [YNIMTool nilFilter:self.senderName],
                 [YNIMTool nilFilter:self.sendNo],
                 [YNIMTool nilFilter:self.dataTime],
                 [YNIMTool nilFilter:self.topTime],
                 [YNIMTool nilFilter:self.headUrl],
                 [YNIMTool nilFilter:self.headLocalPath],
                 [NSNumber numberWithInt:self.isTop],
                 [NSNumber numberWithInt:self.unReadCount],
                 [YNIMTool nilFilter:self.messageBody],
                 [NSNumber numberWithInt:self.sendState],
                 [YNIMTool nilFilter:self.text],
                 [NSNumber numberWithInt:self.messageType],
                 [NSNumber numberWithInt:self.isNewListPullData],
                 [NSNumber numberWithInt:self.masterRecentType],
                 [YNIMTool nilFilter:self.packId],
                 [YNIMTool nilFilter:self.aitContactMarkArray],
                 [NSNumber numberWithBool:self.isMessageWithdraw],
                 [YNIMTool nilFilter:self.more],
                 [YNIMTool nilFilter:self.userNo]];
    }else{
        return @[[YNIMTool nilFilter:self.title],
                 [YNIMTool nilFilter:self.content],
                 [NSNumber numberWithInt:self.chatType],
                 [YNIMTool nilFilter:self.senderName],
                 [YNIMTool nilFilter:self.sendNo],
                 [YNIMTool nilFilter:self.dataTime],
                 [YNIMTool nilFilter:self.headUrl],
                 [YNIMTool nilFilter:self.headLocalPath],
                 [NSNumber numberWithInt:self.unReadCount],
                 [YNIMTool nilFilter:self.messageBody],
                 [NSNumber numberWithInt:self.sendState],
                 [YNIMTool nilFilter:self.text],
                 [NSNumber numberWithInt:self.messageType],
                 [NSNumber numberWithInt:self.isNewListPullData],
                 [NSNumber numberWithInt:self.masterRecentType],
                 [YNIMTool nilFilter:self.packId],
                 [YNIMTool nilFilter:self.aitContactMarkArray],
                 [NSNumber numberWithBool:self.isMessageWithdraw],
                 [YNIMTool nilFilter:self.more],
                 [YNIMTool nilFilter:self.userNo]];
    }
}

+ (BOOL)createRecentChatTableInDatabase:(FMDatabase *)db
{
    if ([db tableExists:[YNRecentChatObejct tableName]]) {
        return YES;
    }
    
    NSString *createRecentChatSql = [NSString stringWithFormat:@"%@%@(%@ INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, %@ varchar(30), %@ varchar(100), %@ TEXT, %@ INTEGER, %@ varchar(30), %@ varchar(30), %@ varchar(30), %@ varchar(30), %@ varchar(100), %@ varchar(100), %@ INTEGER, %@ INTEGER, %@ varchar(200), %@ INTEGER, %@ varchar(100), %@ INTEGER, %@ INTEGER, %@ INTEGER, %@ varchar(30), %@ varchar(150), %@ INTEGER, %@ varchar)", CREATE_TABLE,
                                     
                                     [YNRecentChatObejct tableName],
                                     [YNRecentChatObejct ID],
                                     [YNRecentChatObejct userNo],
                                     [YNRecentChatObejct title],
                                     [YNRecentChatObejct content],
                                     [YNRecentChatObejct chatType],
                                     [YNRecentChatObejct senderName],
                                     [YNRecentChatObejct sendNo],
                                     [YNRecentChatObejct dataTime],
                                     [YNRecentChatObejct topTime],
                                     [YNRecentChatObejct headUrl],
                                     [YNRecentChatObejct headLocalPath],
                                     [YNRecentChatObejct isTop],
                                     [YNRecentChatObejct unReadCount],
                                     [YNRecentChatObejct messageBody],
                                     [YNRecentChatObejct sendState],
                                     [YNRecentChatObejct text],
                                     [YNRecentChatObejct messageType],
                                     [YNRecentChatObejct isNewListPullData],
                                     [YNRecentChatObejct masterRecentType],
                                     [YNRecentChatObejct packId],
                                     [YNRecentChatObejct aitContactMarkArray],
                                     [YNRecentChatObejct isMessageWithdraw],
                                     [YNRecentChatObejct more]];
    BOOL result = [db executeUpdate:createRecentChatSql];
    return result;
}


/**JC** 在表中新增一个列（字段） **/
+ (BOOL)insertRecentChatColumnInDatabase:(FMDatabase*)db MessageContinuous:(NSString*)MessageContinuous columnDataType:(NSString*)type{
    if (![db columnExists:MessageContinuous inTableWithName:[YNRecentChatObejct tableName]]) {
        //列name在表中不存在，所以加入
        BOOL result= [db executeUpdate:[NSString stringWithFormat:@"%@ %@ add %@ %@",ALTER_TABLE,[YNRecentChatObejct tableName],MessageContinuous,type]];
        return result;
    }
    return YES;
}

+ (BOOL)determineRecentChatTableWithUserNo:(NSString *)UserNo Database:(FMDatabase *)db
{
    NSString *queryUserNoSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ LIKE '%@'",
                                [YNRecentChatObejct tableName],
                                [YNRecentChatObejct userNo],
                                UserNo];
    FMResultSet *rs = [db executeQuery:queryUserNoSql];
    
    BOOL result = NO;
    while ([rs next])
    {
        result = YES;
    }
    [rs close];
    
    return result;
}

- (BOOL)insertRecentChatTableInDatabase:(FMDatabase *)db
{
    if (self.isTop == kFlag_UnKnowTop)
    {
        self.isTop = kFlag_UnTop;
    }
    NSString *fieldString = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@",
                             [YNRecentChatObejct title],
                             [YNRecentChatObejct content],
                             [YNRecentChatObejct chatType],
                             [YNRecentChatObejct senderName],
                             [YNRecentChatObejct sendNo],
                             [YNRecentChatObejct dataTime],
                             [YNRecentChatObejct topTime],
                             [YNRecentChatObejct headUrl],
                             [YNRecentChatObejct headLocalPath],
                             [YNRecentChatObejct isTop],
                             [YNRecentChatObejct unReadCount],
                             [YNRecentChatObejct messageBody],
                             [YNRecentChatObejct sendState],
                             [YNRecentChatObejct text],
                             [YNRecentChatObejct messageType],
                             [YNRecentChatObejct isNewListPullData],
                             [YNRecentChatObejct masterRecentType],
                             [YNRecentChatObejct packId],
                             [YNRecentChatObejct aitContactMarkArray],
                             [YNRecentChatObejct isMessageWithdraw],
                             [YNRecentChatObejct more],
                             [YNRecentChatObejct userNo]];
    
    if (self.unReadCount<0) {
        self.unReadCount=0;
    }
    NSString *insertRecentChatSql = [NSString stringWithFormat:@"%@%@ (%@) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", INSERT_TABLE, [YNRecentChatObejct tableName], fieldString];
    
    BOOL result = [db executeUpdate:insertRecentChatSql withArgumentsInArray:[self insertParam]];
    
    return result;
}

- (BOOL)updateRecentChatTableInDatabase:(FMDatabase *)db isFromRoamingSource:(BOOL)isRoaming
{
    if (self.isTop == kFlag_UnKnowTop)
    {
        //是接收的消息
        NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?,%@ = ?,%@ = ?, %@ = ?, %@ = ?, %@ = ? WHERE %@ = ?",
                                         [YNRecentChatObejct tableName],
                                         [YNRecentChatObejct title],
                                         [YNRecentChatObejct content],
                                         [YNRecentChatObejct chatType],
                                         [YNRecentChatObejct senderName],
                                         [YNRecentChatObejct sendNo],
                                         [YNRecentChatObejct dataTime],
                                         [YNRecentChatObejct headUrl],
                                         [YNRecentChatObejct headLocalPath],
                                         [YNRecentChatObejct unReadCount],
                                         [YNRecentChatObejct messageBody],
                                         [YNRecentChatObejct sendState],
                                         [YNRecentChatObejct text],
                                         [YNRecentChatObejct messageType],
                                         [YNRecentChatObejct isNewListPullData],
                                         [YNRecentChatObejct masterRecentType],
                                         [YNRecentChatObejct packId],
                                         [YNRecentChatObejct aitContactMarkArray],
                                         [YNRecentChatObejct isMessageWithdraw],
                                         [YNRecentChatObejct more],
                                         [YNRecentChatObejct userNo]];
        
        if (self.unReadCount > 100)
        {
            self.unReadCount = 100;
        }
        else
        {
            if (!isRoaming) {
                self.unReadCount = [YNRecentChatObejct queryUnReadWithUserNo:self.userNo inDatabase:db] + self.unReadCount;
            }
        }
        if (self.unReadCount<0) {
            self.unReadCount=0;
        }
        
        BOOL result = [db executeUpdate:updateRecentChatSql withArgumentsInArray:[self updateParamIsTop:NO]];
        return result;
    }
    else
    {
        //置顶的消息
        NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ? ,%@ = ?,%@ = ?,%@ = ? WHERE %@ = ?",
                                         [YNRecentChatObejct tableName],
                                         [YNRecentChatObejct title],
                                         [YNRecentChatObejct content],
                                         [YNRecentChatObejct chatType],
                                         [YNRecentChatObejct senderName],
                                         [YNRecentChatObejct sendNo],
                                         [YNRecentChatObejct dataTime],
                                         [YNRecentChatObejct topTime],
                                         [YNRecentChatObejct headUrl],
                                         [YNRecentChatObejct headLocalPath],
                                         [YNRecentChatObejct isTop],
                                         [YNRecentChatObejct unReadCount],
                                         [YNRecentChatObejct messageBody],
                                         [YNRecentChatObejct sendState],
                                         [YNRecentChatObejct text],
                                         [YNRecentChatObejct messageType],
                                         [YNRecentChatObejct isNewListPullData],
                                         [YNRecentChatObejct masterRecentType],
                                         [YNRecentChatObejct packId],
                                         [YNRecentChatObejct aitContactMarkArray],
                                         [YNRecentChatObejct isMessageWithdraw],
                                         [YNRecentChatObejct more],
                                         [YNRecentChatObejct userNo]];
        
        self.unReadCount = [YNRecentChatObejct queryUnReadWithUserNo:self.userNo inDatabase:db] + self.unReadCount;
        
        BOOL result = [db executeUpdate:updateRecentChatSql withArgumentsInArray:[self updateParamIsTop:YES]];
        
        return result;
    }
    
}

/**HX** 将userNo未读消息的未读数做加一或减一的更新 **/
+(BOOL)updateUserUnreadCountByOneWithUserNo:(NSString*)UserNo isAddOne:(BOOL)addOne Database:(FMDatabase*)db{
    int saveUnreadCount=[YNRecentChatObejct queryUnReadWithUserNo:UserNo inDatabase:db];
    if (addOne) {
        saveUnreadCount+=1;
    }else{
        saveUnreadCount-=1;
    }
    BOOL result=NO;
    if (saveUnreadCount<0) {
        saveUnreadCount=0;
    }
    NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@'",
                                     [YNRecentChatObejct tableName],
                                     [YNRecentChatObejct unReadCount],
                                     [NSNumber numberWithInt:saveUnreadCount],
                                     [YNRecentChatObejct userNo],
                                     UserNo];
    result = [db executeUpdate:updateRecentChatSql];
    return result;
}

/**JC** 将isNewListPullData更新为YES，表示已经拉取过消息漫游 **/
+(BOOL)updateRecentNewListPullDataUnreadCountByOneWithUserNo:(NSString*)UserNo Database:(FMDatabase*)db{
    
    BOOL result =NO;
    NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@'",
                                     [YNRecentChatObejct tableName],
                                     [YNRecentChatObejct isNewListPullData],
                                     [NSNumber numberWithInt:YES],
                                     [YNRecentChatObejct userNo],
                                     UserNo];
    result = [db executeUpdate:updateRecentChatSql];
    return result;
}


//将UserNo未读消息的未读条数置为0，若UserNo 为nil，则将全部未读消息条数置为0
+ (BOOL)unReadNumberMakeZeroWithUserNo:(NSString *)UserNo Database:(FMDatabase *)db
{
    if (UserNo != nil)
    {
        NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@' AND %@ > '%@'",
                                         [YNRecentChatObejct tableName],
                                         [YNRecentChatObejct unReadCount],
                                         [NSNumber numberWithInt:0],
                                         [YNRecentChatObejct userNo],
                                         UserNo,
                                         [YNRecentChatObejct unReadCount],
                                         [NSNumber numberWithInt:0]];
        
        
        BOOL result = [db executeUpdate:updateRecentChatSql];
        
        return result;
    }
    else
    {
        NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ > '%@'",
                                         [YNRecentChatObejct tableName],
                                         [YNRecentChatObejct unReadCount],
                                         [NSNumber numberWithInt:0],
                                         [YNRecentChatObejct unReadCount],
                                         [NSNumber numberWithInt:0]];
        
        BOOL result = [db executeUpdate:updateRecentChatSql];
        
        return result;
        
    }
    
}

/**
 2019-01 修改 查询所有最近会话消息列表数据

 @param db <#db description#>
 @param needScreenTop 是否需要赛选处理置顶消息
 @return 集合
 */
+ (NSArray<YNRecentChatObejct*> *)queryAllRecentChatTableInDatabase:(FMDatabase *)db needScreenTopChat:(BOOL)needScreenTop
{
    NSMutableArray *recentArray = [NSMutableArray array];
    if (!needScreenTop) {
        NSString *queryAllRecentUnTopSql = [NSString stringWithFormat:@"SELECT * FROM %@ ORDER BY %@ DESC",
                                            [YNRecentChatObejct tableName],
                                            [YNRecentChatObejct dataTime]];
        
        FMResultSet *rsUnTop = [db executeQuery:queryAllRecentUnTopSql];
        while ([rsUnTop next])
        {
            YNRecentChatObejct *recentChat = [[YNRecentChatObejct alloc] init];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[rsUnTop resultDictionary]];
            [dic removeObjectForKey:[YNRecentChatObejct ID]];
            if (![YNIMTool isEmptyDictionary:dic]) {
                [recentChat setValuesForKeysWithDictionary:dic];
            }
            //将通知公告筛选排第一个
            if (recentChat.chatType == YNBubbleMessageChatTypeNoticeAnnouncement) {
                [recentArray insertObject:recentChat atIndex:0];
            }else{
                [recentArray addObject:recentChat];
            }
        }
        [rsUnTop close];
        
    }else{
        NSString *queryAllRecentSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = %@ ORDER BY %@ DESC",
                                       [YNRecentChatObejct tableName],
                                       [YNRecentChatObejct isTop],
                                       [NSNumber numberWithInt:1],
                                       [YNRecentChatObejct topTime]];
        FMResultSet *rs = [db executeQuery:queryAllRecentSql];
        
        while ([rs next])
        {
            
            YNRecentChatObejct *recentChat = [[YNRecentChatObejct alloc] init];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[rs resultDictionary]];
            [dic removeObjectForKey:[YNRecentChatObejct ID]];
            if (![YNIMTool isEmptyDictionary:dic]) {
                [recentChat setValuesForKeysWithDictionary:dic];
            }
            //将通知公告筛选排第一个
            if (recentChat.chatType == YNBubbleMessageChatTypeNoticeAnnouncement) {
                [recentArray insertObject:recentChat atIndex:0];
            }else{
                [recentArray addObject:recentChat];
            }
        }
        [rs close];
        
        NSString *queryAllRecentUnTopSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = %@ ORDER BY %@ DESC",
                                            [YNRecentChatObejct tableName],
                                            [YNRecentChatObejct isTop],
                                            [NSNumber numberWithInt:0],
                                            [YNRecentChatObejct dataTime]];
        
        FMResultSet *rsUnTop = [db executeQuery:queryAllRecentUnTopSql];
        while ([rsUnTop next])
        {
            YNRecentChatObejct *recentChat = [[YNRecentChatObejct alloc] init];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[rsUnTop resultDictionary]];
            [dic removeObjectForKey:[YNRecentChatObejct ID]];
            if (![YNIMTool isEmptyDictionary:dic]) {
                [recentChat setValuesForKeysWithDictionary:dic];
            }
            //将通知公告筛选排第一个
            if (recentChat.chatType == YNBubbleMessageChatTypeNoticeAnnouncement) {
                [recentArray insertObject:recentChat atIndex:0];
            }else{
                [recentArray addObject:recentChat];
            }
        }
        [rsUnTop close];
        
    }
    return recentArray;
}

+ (BOOL)deleteRecentForTableWithUserNo:(NSString *)UserNo ChatType:(int)ChatType database:(FMDatabase *)db;
{
    NSString *deleteRecentChatSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", [YNRecentChatObejct tableName], [YNRecentChatObejct userNo]];
    BOOL result = [db executeUpdate:deleteRecentChatSql,UserNo];
    return result;
}

+ (BOOL)deleteRecentSystemAndInformChatWithdatabase:(FMDatabase *)db;
{
    if (![db tableExists:[YNRecentChatObejct tableName]]) {
        return YES;
    }
    NSString *deleteRecentChatSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = %@ OR %@ = %@", [YNRecentChatObejct tableName],[YNRecentChatObejct chatType],[NSNumber numberWithInteger:YNBubbleMessageChatTypeNoticeAnnouncement],[YNRecentChatObejct chatType],[NSNumber numberWithInteger:YNBubbleMessageChatTypeSystemMessage]];
    BOOL result = [db executeUpdate:deleteRecentChatSql];
    return result;
}

+ (BOOL)deleteAllRecentWithDatabase:(FMDatabase *)db
{
    NSString *deleteAllRecentSql= [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ <> '广播'",[YNRecentChatObejct tableName],[YNRecentChatObejct userNo]];
    
    BOOL result = [db executeUpdate:deleteAllRecentSql];
    
    return result;
}

//获取当前UserID的未读消息记录
+ (int)queryUnReadWithUserNo:(NSString *)userNo inDatabase:(FMDatabase *)db;
{
    NSString *queryUnreadSql = [NSString stringWithFormat:@"SELECT %@ FROM %@ WHERE %@ = ?",
                                [YNRecentChatObejct unReadCount],
                                [YNRecentChatObejct tableName],
                                [YNRecentChatObejct userNo]];
    
    FMResultSet *rs = [db executeQuery:queryUnreadSql,userNo];
    
    int UnRead = 0;
    
    while ([rs next])
    {
        UnRead = [rs intForColumn:[YNRecentChatObejct unReadCount]];
    }
    
    [rs close];
    
    return UnRead;
}

//根据userNo查询本条信息
+ (NSArray *)queryRecentChatWithUserNo:(NSString *)userNo inDatabase:(FMDatabase *)db
{
    NSMutableArray * recentArray = [NSMutableArray array];
    NSString *queryUnreadSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@'",
                                [YNRecentChatObejct tableName],
                                [YNRecentChatObejct userNo],
                                userNo];
    
    FMResultSet *rsUnTop = [db executeQuery:queryUnreadSql];
    while ([rsUnTop next])
    {
        YNRecentChatObejct *recentChat = [[YNRecentChatObejct alloc] init];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[rsUnTop resultDictionary]];
        [dic removeObjectForKey:[YNRecentChatObejct ID]];
        if (![YNIMTool isEmptyDictionary:dic]) {
            [recentChat setValuesForKeysWithDictionary:dic];
        }
        
        
        [recentArray addObject:recentChat];
    }
    [rsUnTop close];
    
    return recentArray;
}

//根据packId查询本条信息
+ (NSArray *)queryRecentChatWithPackId:(NSString *)packId inDatabase:(FMDatabase *)db
{
    NSMutableArray * recentArray = [NSMutableArray array];
    NSString *queryUnreadSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@'",
                                [YNRecentChatObejct tableName],
                                [YNRecentChatObejct packId],
                                packId];
    
    FMResultSet *rsUnTop = [db executeQuery:queryUnreadSql];
    while ([rsUnTop next])
    {
        YNRecentChatObejct *recentChat = [[YNRecentChatObejct alloc] init];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[rsUnTop resultDictionary]];
        [dic removeObjectForKey:[YNRecentChatObejct ID]];
        if (![YNIMTool isEmptyDictionary:dic]) {
            [recentChat setValuesForKeysWithDictionary:dic];
        }
        
        
        [recentArray addObject:recentChat];
    }
    [rsUnTop close];
    
    return recentArray;
}

/**
 *  @brief  更改聊天记录标题
 *
 *  @param title  最近聊天记录标题
 *  @param userNo
 *  @param db
 *
 *  @return
 */
+ (BOOL)updateRecentChatTitle:(NSString *)title byUserNo:(NSString *)userNo inDatabase:(FMDatabase *)db
{
    NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@'",
                                     [YNRecentChatObejct tableName],
                                     [YNRecentChatObejct title],
                                     title,
                                     [YNRecentChatObejct userNo],
                                     userNo];
    
    BOOL result = [db executeUpdate:updateRecentChatSql];
    
    
    return result;
    
}


//更新会话
+ (BOOL)updateRecentChatTableWithUseNo:(NSString *)UserNo sendTime:(NSString *)sendTime isSuccess:(int)messageState inDatabase:(FMDatabase *)db
{
    NSString *updateRecentChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = %@ WHERE %@ = '%@'",
                                     [YNRecentChatObejct tableName],
                                     [YNRecentChatObejct dataTime],
                                     sendTime,
                                     [YNRecentChatObejct sendState],
                                     [NSNumber numberWithInt:messageState],
                                     [YNRecentChatObejct userNo],
                                     UserNo];
    
    BOOL result = [db executeUpdate:updateRecentChatSql];
    return  result;
}

//根据ID查询一条最近会话
+ (YNRecentChatObejct *)queryRecentChatWithFriendId:(NSString *)friendId inDatabase:(FMDatabase *)db
{
    NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@'",
                          [YNRecentChatObejct tableName],
                          [YNRecentChatObejct userNo],
                          friendId];
    NSMutableArray *recentArray = [NSMutableArray array];
    
    FMResultSet *rs = [db executeQuery:querySql];
    while ([rs next])
    {
        YNRecentChatObejct *recentChat = [[YNRecentChatObejct alloc] init];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[rs resultDictionary]];
        [dic removeObjectForKey:[YNRecentChatObejct ID]];
        if (![YNIMTool isEmptyDictionary:dic]) {
            [recentChat setValuesForKeysWithDictionary:dic];
        }
        
        [recentArray addObject:recentChat];
    }
    
    [rs close];
    if (recentArray.count > 0)
    {
        return [recentArray lastObject];
    }
    return nil;
}

//只更新状态
+ (BOOL)updateMessageSendingStateWithDatabase:(FMDatabase *)db
{
    NSString *updateMessageSendingStateSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = %@",
                                              [YNRecentChatObejct tableName],
                                              [YNRecentChatObejct sendState],
                                              [NSNumber numberWithInt:YNMessageSendStateFailed],
                                              [YNRecentChatObejct sendState],
                                              [NSNumber numberWithInt:YNMessageSendStateSending]];
    BOOL result = [db executeUpdate:updateMessageSendingStateSql];
    return result;
}

/**
 修改撤回状态  2018.11.27jc添加
 
 @param chatId 对方id
 @param packId 消息id
 @param db db
 @return
 */
+(BOOL)updateRecentChatWithdrawWithChatId:(NSString *)chatId packId:(NSString *)packId inDatabase:(FMDatabase *)db
{
    //未读消息，先不减1，因为opfire没有减1
//    int saveUnreadCount=[YNRecentChatObejct queryUnReadWithUserNo:chatId inDatabase:db];
//    saveUnreadCount-=1;
//    if (saveUnreadCount<0) {
//        saveUnreadCount=0;
//    }
//
//    //修改未读计数
//    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET  %@ = %d WHERE %@ = '%@'",
//                                        [YNRecentChatObejct tableName],
//                                        [YNRecentChatObejct unReadCount],
//                                        saveUnreadCount,
//                                        [YNRecentChatObejct userNo],
//                                        chatId];
//
//    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    
    
        
    NSString * updateTwoPeopleChatWithdrawSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE  %@ = '%@'",
                                      [YNRecentChatObejct tableName],
                                      [YNRecentChatObejct isMessageWithdraw],
                                      [NSNumber numberWithBool:YES],
                                      [YNRecentChatObejct packId],
                                      packId];
    
   BOOL result = [db executeUpdate:updateTwoPeopleChatWithdrawSql];
    
    return  result;
}


@end
